#include "n32g031.h"
#include "DevicesPower.h"



void vPowerInit(void)
{
    GPIO_InitType GPIO_InitStructure = {0};

    /* -1- Enable GPIOx Clock (to be able to program the configuration registers) */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);


    vPowerClose(POWER_ALL);

    /* -2- Configure GPIOx_PIN in output push-pull mode */
    GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin = POWER_12V_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.GPIO_Pull = GPIO_NO_PULL;
    GPIO_InitPeripheral(POWER_12V_GPIO_Port, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = POWER_3_3V_Pin;
    GPIO_InitPeripheral(POWER_3_3V_GPIO_Port, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = POWER_CHARTE_20V_Pin;
    GPIO_InitPeripheral(POWER_CHARTE_20V_GPIO_Port, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = POWER_CHARTE_3_3V_Pin;
    GPIO_InitPeripheral(POWER_CHARTE_3_3V_GPIO_Port, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = POWER_AIR_DRYING_Pin;
    GPIO_InitPeripheral(POWER_AIR_DRYING_GPIO_Port, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = POWER_DUST_HOOD_Pin;
    GPIO_InitPeripheral(POWER_DUST_HOOD_GPIO_Port, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = POWER_PTC_Pin;
    GPIO_InitPeripheral(POWER_PTC_GPIO_Port, &GPIO_InitStructure);


    vPowerOpen(POWER_12V | POWER_3_3V);
}

void vPowerOpen(uint16_t usNumber)
{
    if(usNumber & POWER_12V)
        POWER_12V_ENABLE();

    if(usNumber & POWER_3_3V)
        POWER_3_3V_ENABLE();

    if(usNumber & POWER_CHARTE_20V)
        POWER_CHARTE_20V_ENABLE();

    if(usNumber & POWER_CHARTE_3_3V)
        POWER_CHARTE_3_3V_ENABLE();

    if(usNumber & POWER_AIR_DRYING)
        POWER_AIR_DRYING_ENABLE();

    if(usNumber & POWER_DUST_HOOD)
        POWER_DUST_HOOD_ENABLE();

    if(usNumber & POWER_PTC)
        POWER_PTC_ENABLE();
}

void vPowerClose(uint16_t usNumber)
{
    if(usNumber & POWER_12V)
        POWER_12V_DISABLE();

    if(usNumber & POWER_3_3V)
        POWER_3_3V_DISABLE();

    if(usNumber & POWER_CHARTE_20V)
        POWER_CHARTE_20V_DISABLE();

    if(usNumber & POWER_CHARTE_3_3V)
        POWER_CHARTE_3_3V_DISABLE();

    if(usNumber & POWER_AIR_DRYING)
        POWER_AIR_DRYING_DISABLE();

    if(usNumber & POWER_DUST_HOOD)
        POWER_DUST_HOOD_DISABLE();

    if(usNumber & POWER_PTC)
        POWER_PTC_DISABLE();
}
